package com.lenovo.spider;

import cn.hutool.core.io.resource.ClassPathResource;
import com.lenovo.spider.component.TaskIterator;
import com.lenovo.spider.domain.Request;
import com.lenovo.spider.domain.Task;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import static com.lenovo.spider.component.TaskProcess.Downloader.Type.HTTP;

/**
 * @author <a href="mailto:liuhan3@lenovo.com">HanL(liuhan3)</a>
 * on 2018/4/11 22:19
 */
public class Main {


    static TaskIterator getIterator(String name, int limit) {
        return new TaskIterator() {
            public final String itName = name;
            private int size = Math.min(Math.max(0, limit), Integer.MAX_VALUE);

            @Override
            public void addTask(Task task) {

            }

            @Override
            public boolean hasNext() {
                return size != 0;
            }

            @Override
            public Task next() {
                size--;
                Request request = Request.builder().url("http://www.baidu.com/" )
//                        .downloaderType(((size % 2) == 0) ? HTTP : WEBDRIVER)
                        .downloaderType(HTTP)
                        .build();
                return Task.create(request);
            }
        };
    }

    public static InputStream getResourceAsStream(String path) {
        return new ClassPathResource(path).getStream();
    }

    public static void main(String[] args) throws IOException {

//         SpiderEngine engine = SpiderEngine.get();
//        engine.createSpider(() -> new Spider(getIterator("it1", 1)));
//        engine.startAllSpider();
//
//        engine.startAllSpider();
//        ThreadUtil.sleep(500);
//        engine.stopAllSpider();
//        ThreadUtil.sleep(2000);
//
//        engine.createSpider(() -> new Spider(getIterator("it2", 4)));
//        engine.startAllSpider();
        SpiderEngine engine = SpiderEngine.get();
        new Spider(getIterator("it1", 1)).run();

        System.out.println(new BufferedReader(new InputStreamReader(System.in)).readLine());
    }
}
